#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define ERROR -1

typedef int Position;
typedef struct SNode *PtrToSNode;
struct SNode {
    double* data;
    Position top;
    int MaxSize;
};
typedef PtrToSNode Stack;


Stack CreateStack(int MaxSize)
{
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->data = (double *)malloc(MaxSize * sizeof(double));
    S->top = -1;
    S->MaxSize = MaxSize;
    return S;
}

bool IsFull(Stack S)
{
    return (S->top == S->MaxSize - 1);
}

bool Push(Stack S, double X)
{
    if (IsFull(S))
    {
        printf("Stack is full !");
        return false;
    }
    else 
    {
        S->data[++(S->top)] = X;
        return true;
    }
}

bool IsEmpty(Stack S)
{
    return (S->top == -1);
}

double Pop(Stack S)
{
    if (IsEmpty(S))
    {
        printf("Stack is empty !");
        return ERROR;
    }
    else
        return (S->data[(S->top)--]);
}


int main()
{
    int MaxSize = 10;

    Stack example_stack = CreateStack(MaxSize);
    double number;

    for (;;)
    {
        printf("Enter the number to input(0 to finish) : ");
        scanf("%lf", &number);
        if (number == 0)
            break;
        if(Push(example_stack, number))
            printf("Insert Success !\n");
    }

    printf("\nThe stack have %d numbers\n", example_stack->top + 1);
    for (int index = 0; index <= example_stack->top; index++)
        printf("%lf ", example_stack->data[index]);
    printf("\n\n");

    char code;
    for (;;)
    {
        printf("Pop the stack ? (Y/N)\n");
        scanf(" %c", &code);
        if (code == 'Y' || code == 'y')
            printf("%lf\n", Pop(example_stack));
        else
            break;
    }

    printf("\nThe stack have %d numbers\n", example_stack->top + 1);
    for (int index = 0; index <= example_stack->top; index++)
        printf("%lf ", example_stack->data[index]);

    return 0;
}